/**
   VideomessageController
 * Time:2018年5月25日
 */
package com.ssm.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.enterprise.inject.Model;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.format.Printer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.entity.Hotmsg;
import com.ssm.entity.Page;
import com.ssm.entity.PageCount;
import com.ssm.entity.Videomessage;
import com.ssm.entity.Videoshowarea;
import com.ssm.entity.vo.PageNumVo;
import com.ssm.entity.vo.PieChartVo;
import com.ssm.entity.vo.SeachByColumn;
import com.ssm.entity.vo.VideoMessageVo;
import com.ssm.entity.vo.VideomessageTypeVo;
import com.ssm.entity.vo.VideoshowVo;
import com.ssm.service.FileService;
import com.ssm.service.HotService;
import com.ssm.service.TypeService;
import com.ssm.service.VideoMessageService;
import com.ssm.util.QiniuTestUtil;

/**
 * @author 15199
 *
 */
@Controller
@RequestMapping("/videomessage")
@SuppressWarnings("all")
public class VideomessageController extends BaseController {
	@Resource VideoMessageService videoMessageService;
	@Resource TypeService typeService;
	@Resource FileService fileService;
	@Resource HotService hotService;
	private int pageMax;
	/*
	 *列表显示首页的内容 
	 */
	@RequestMapping("/list.do")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		List<VideoMessageVo> list=videoMessageService.getVmBycondition(new Page(0,5),new SeachByColumn()); 
		List<VideoMessageVo> list1=videoMessageService.getCount();
		ModelAndView mav=new ModelAndView("/AE/show");
		mav.addObject("list",list);
		int count=list1.get(0).getCount();
		PageCount pageCount=new PageCount(count,5);
		mav.addObject("pageNum",1);
		mav.addObject("pageMax",pageCount.getPageMax());
		pageMax=pageCount.getPageMax();
		return mav;
	}

	@RequestMapping("/data.do" )
	public ModelAndView data(String videoid,String videoname,HttpServletRequest request,HttpServletResponse response){
		String pageNum=request.getParameter("pageNum");
		SeachByColumn seachByColumn=new SeachByColumn();

		if(videoid!=null && videoid!=""){	
			seachByColumn.setVideoid(Integer.parseInt(videoid));
		}

		if(videoname!=null && videoid!=""){
			seachByColumn.setVideoname(videoname);
		}

		List<VideoMessageVo> list=videoMessageService.getVmBycondition(new Page((Integer.parseInt(pageNum)-1)*5,5),seachByColumn); 
		ModelAndView mav=new ModelAndView("/AE/show");
		mav.addObject("list",list);
		mav.addObject("pageNum",Integer.parseInt(pageNum));
		mav.addObject("pageMax",pageMax);
		return mav;
	}
	@RequestMapping("/get.do")
	public ModelAndView get(String videoid){

		List<VideoMessageVo> list=videoMessageService.getById(Integer.parseInt(videoid));     
		ModelAndView mav=new ModelAndView("/AE/showByid");
		mav.addObject("list",list);

		return mav;
	}

	@RequestMapping("/update.do")
	public void update(@RequestParam("videoid") int videoid, 
			@RequestParam("videoname") String  videoname,
			@RequestParam("videorecommend") String videorecommend,
			String home,
			String vouch,
			String top,
			@RequestParam("videotype") String videotype,
			@RequestParam("labelname") String labelname,
			@RequestParam("hot") int hot,
			HttpServletResponse response) throws IOException{
		String Line;
		Line=home+vouch+top;
		Line=Line.replace("null","");




		VideoMessageVo videoMessageVo=new VideoMessageVo(videoid,videoname, videorecommend,Line, videotype, labelname, hot);
		videoMessageService.updateBycondition(videoMessageVo);

		response.sendRedirect("/videomessage/list.do");
	}

	@RequestMapping("/create.do")
	public ModelAndView insert(
			@RequestParam("videoname") String  videoname,
			@RequestParam("videorecommend") String videorecommend,
			String home,
			String vouch,
			String top,
			@RequestParam("videotype") String videotype,
			@RequestParam("labelname") String labelname,
			@RequestParam("hot") int hot,
			HttpServletResponse response) throws IOException{

		String Line;
		Line=home+vouch+top;
		Line=Line.replace("null","");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		String time=sdf.format(date);

		VideoMessageVo videoMessageVo=new VideoMessageVo(videoname, videorecommend,time,Line, videotype, labelname, hot);
		videoMessageService.insert(videoMessageVo);
		ModelAndView mav=new ModelAndView("/AE/upload");
		mav.addObject("videoname",videoname);

		return mav; 

	}
	//视频文件上传
	@RequestMapping("/upload.do")
	public void upload(@RequestParam String username,@RequestParam MultipartFile uploadFile,HttpServletRequest request,HttpServletResponse response) throws IOException{

		//得到真实路径
		String targetDir=request.getSession().getServletContext().getRealPath("/upload");
		fileService.qiniuFileupload(username, uploadFile, request);
		response.sendRedirect("/videomessage/list.do");

	}

	@RequestMapping("/updateState.do")
	public void updateState(HttpServletRequest request,HttpServletResponse response) throws IOException{

		String id=request.getParameter("id");

		videoMessageService.updateById(Integer.parseInt(id));

		response.sendRedirect("/videomessage/list.do");


	}

	//获取主页的内容
	@RequestMapping("/getHomePage.do")
	public ModelAndView getHomePage(HttpServletRequest request){

		int pagesize=5;

		List<VideoshowVo> list=new ArrayList<VideoshowVo>();

		List<VideoshowVo> list1=new ArrayList<VideoshowVo>();



		list=videoMessageService.getHomePage(new Page());

		String pageNum=request.getParameter("pageNum");

		PageCount pageCount=new PageCount(list.size(),5);

		pageMax=pageCount.getPageMax();

		if(org.springframework.util.StringUtils.isEmpty(pageNum)){


			list1=videoMessageService.getHomePage(new Page(0,5));


		}else{


			list1=videoMessageService.getHomePage(new Page((Integer.parseInt(pageNum)-1)*pagesize,pagesize));


		}


		ModelAndView mav=new ModelAndView("/AE/FirstPage");

		mav.addObject("list",list1);

		if(pageNum!=null){

			mav.addObject("pageNum",Integer.parseInt(pageNum));

		}else{

			mav.addObject("pageNum","1");	

		}


		mav.addObject("pageMax",pageMax);

		return mav;


	}


	//跳转并加载首页的大图内容
	@RequestMapping("/getFirstPage.do")
	@ResponseBody
	public ModelAndView getFirstPage(){
		ModelAndView mav=new ModelAndView("/FG/index");
		List<VideoMessageVo> list=videoMessageService.getTop(new Page(0,1));
		mav.addObject("list", list);
		return mav;
	}
	//获取首页置顶内容
	@RequestMapping("/getFirstPage_Top.do")
	@ResponseBody
	public List<VideoMessageVo> getFirstPage_Top(){
		List<VideoMessageVo> list=videoMessageService.getTop(new Page(1,4));
		return list;	
	}

	//获取首页二次元内容
	@RequestMapping("/getFirstPage_anime.do")
	@ResponseBody
	public List<VideoMessageVo> getFirstPage_anime(@RequestBody String parmes){
		List<VideoMessageVo> list=videoMessageService.get_Top(new Page(0,8),parmes);
		return list;

	}

	//视频播放
	@RequestMapping("/player.do")
	public ModelAndView player(@RequestParam("id") Integer id,HttpServletRequest request) throws UnsupportedEncodingException{
		String videoname=null;
		int hot=0;
		ModelAndView mav=new ModelAndView("/FG/single");
		List<VideoMessageVo> list=videoMessageService.getById(id);
		for (VideoMessageVo videoMessageVo : list) {
			videoname=videoMessageVo.getVideoname();
			hot=videoMessageVo.getHot();
		}
		request.getSession().setAttribute("videoid",id);
		mav.addObject("videoname",videoname);
		mav.addObject("hot",hot);
		return mav;
	}


	//点赞的代码
	@RequestMapping("/dianzan.do")
	@ResponseBody
	public String dianzan(@RequestBody String parmes,HttpServletRequest request,HttpServletResponse response){
		String hot=null;
		if(parmes!=null){
			/*去除字符串的两个引号*/	
			parmes=parmes.replace("\"","");
			videoMessageService.updateHot(Integer.parseInt(parmes));
			List<Hotmsg> list=hotService.getHotById(Integer.parseInt(parmes));
			for (Hotmsg hotmsg : list) {
				hot=String.valueOf(list.get(0).getHot());	
			}
			return hot;
		}else{
			return "false";
		}

	}
	//前台展示所有页面分页
	@RequestMapping("/getAll.do")
	@ResponseBody
	public List<VideoMessageVo> getAll(@RequestBody PageNumVo pageNumVo){
		Page page=parsePageNo1(pageNumVo.getPageNum());
		List<VideoMessageVo> list=videoMessageService.getVmBycondition(page,new SeachByColumn());
		return list;
	}

	//获取最大页数
	@RequestMapping("/getPage.do")
	@ResponseBody
	public int getPageNum(){
		int n=videoMessageService.getPageNum();
		if(n>0){
			return n;
		}else{
			return 0;
		}
	}
	//获取最新的视频
	@RequestMapping("/getLastUpdate.do")
	@ResponseBody
	public List<VideoMessageVo> getLastUpdate(){
		List<VideoMessageVo> list=videoMessageService.getLastUpdate();
		return list;

	}

	//获取最热的视频
	@RequestMapping("/getMostheat.do")
	@ResponseBody
	public List<VideoMessageVo> getMostheat(){
		List<VideoMessageVo> list=videoMessageService.getMostheat();
		return list;
	}

	//获取统计饼图数据
	@RequestMapping("/getPieChartVo.do")
	@ResponseBody
	public List<PieChartVo> getPieChartVo(){
		List<PieChartVo> list=videoMessageService.getDataAnalysis();
		return list;
	}







}
